const { query } = require('../db/index.js');

// 通过事务完成sql 操作，普通查询不需要
// async function getUser({ username }, conn = null) {
//   const sql = 'SELECT * FROM users WHERE username = ?';

//   if (conn) {
//     // 在事务中使用传入的连接
//     const [rows] = await conn.execute(sql, [username]);
//     return rows[0];
//   } else {
//     // 普通查询使用连接池
//     const [user] = await query(sql, [username]);
//     return user;
//   }
// }

async function getUser({ username }) {
  const sql = 'SELECT * FROM users WHERE username = ?';
  const [user] = await query(sql, [username]);
  return user;
}



async function registerUser({ username, name, email, password, salt, profileimage }) {
  const sql = 'INSERT INTO users (username, name, email, password, salt, profileimage) VALUES (?, ?, ?, ?, ?, ?)';
  const { insertId } = await query(sql, [username, name, email, password, salt, profileimage]);
  return insertId;
}

module.exports = {
  getUser,
  registerUser
}
